<?php
namespace app\home\controller;

use app\common\model\HelpGetUpstart;
use app\common\model\UserWalletUpstart;
use app\common\model\UserWalletUpstartFreeze;

use app\common\component\user\UserService;
use app\common\component\help_get_upstart\HelpGetUpstartService;
use app\common\component\user_wallet_upstart\UserWalletUpstartService;
use app\common\component\user_wallet_upstart_log\UserWalletUpstartLogService;
use app\common\component\user_wallet_upstart_freeze\UserWalletUpstartFreezeService;

class WithdrawUpstart extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();

		if(getParam('UPSTART_IS_OPEN') == 0) {
			echo '<meta charset="utf-8"><script>alert("土豪区已关闭!");window.history.go(-1);</script>';exit;
		}
	}

	public function index() {
		$userId = \think\Session::get('user_id');
		$this -> view -> assign('helpGetUpstarts', HelpGetUpstart::where('user_id', $userId) -> order('status ASC, help_get_upstart_id DESC') -> paginate(10, true));

		$resultUserWalletUpstart = (new UserWalletUpstartService) -> getUserWalletUpstartId($userId);
		$this -> view -> assign('user_wallet_upstart', $resultUserWalletUpstart['data']['user_wallet_upstart']);

        return $this -> view -> fetch();
	}

	public function withdraw() {
		$resultUserWalletUpstart = (new UserWalletUpstartService) -> getUserWalletUpstartId(\think\Session::get('user_id'));
		$this -> view -> assign('user_wallet_upstart', $resultUserWalletUpstart['data']['user_wallet_upstart']);

		$resultUser = (new UserService) -> getUser();
    	$this -> view -> assign('user', $resultUser['data']['user']);

		return $this -> view -> fetch();
	}

	public function handleWithdraw() {
		$post = $this -> request -> post();

		$WITHDRAW_RICH_MULTIPLE = getParam('WITHDRAW_RICH_MULTIPLE');
		if(($post['price'] % $WITHDRAW_RICH_MULTIPLE) != 0) {
    		echo '<meta charset="utf-8"><script>alert("必须要以' . $WITHDRAW_RICH_MULTIPLE . '倍起提!");window.history.go(-1);</script>';exit;
    	}

		if($post['price'] > getParam('WITHDRAW_UPSTART_PRICE_MAX')) {
    		echo '<meta charset="utf-8"><script>alert("您提现的' . $post['price'] . '金额大于指定金额!");window.history.go(-1);</script>';exit;
    	}

		$resultCheckPayPassword = (new UserService()) -> checkPayPassword($post['pay_password']);
    	if($resultCheckPayPassword['code'] !== 0) {
    		echo '<meta charset="utf-8"><script>alert("' . $resultCheckPayPassword['message'] . '!");window.history.go(-1);</script>';exit;
    	}

		$userId = \think\Session::get('user_id');

		$resultUserWalletUpstart = (new UserWalletUpstartService) -> getUserWalletUpstartId($userId);
    	if($resultUserWalletUpstart['data']['user_wallet_upstart'] -> price < $post['price']) {
    		echo '<meta charset="utf-8"><script>alert("您的剩余金额不足!");window.history.go(-1);</script>';exit;
    	}
		
		$WITHDRAW_RICH_IN_TIME = getParam('WITHDRAW_RICH_IN_TIME');
		$findHelpGetUpstart = HelpGetUpstart::where('user_id = ' . $userId .' AND status IN(0,1,2) AND begin_time > "' . (time() - ($WITHDRAW_RICH_IN_TIME * 60 * 60)) . '"') -> find();
		if($findHelpGetUpstart) {
			echo '<meta charset="utf-8"><script>alert("您的富豪预约在' . $WITHDRAW_RICH_IN_TIME . '小时内，请耐心等候匹配!");window.history.go(-1);</script>';exit;
		}

		\think\Db::startTrans();
		try {
			$resultUserWalletUpstartLog = (new UserWalletUpstartLogService) -> log(3, 0, $post['price'], $userId, '富豪提现', 1);
			if($resultUserWalletUpstartLog['code'] !== 0) {
				throw new \Exception($resultUserWalletUpstartLog['message']);
			}

			$resultHelpGetUpstart = (new HelpGetUpstartService()) -> withdraw($post['price']);
	    	if($resultHelpGetUpstart['code'] !== 0) {
	    		throw new \Exception($resultHelpGetUpstart['message']);
	    	}

			if(!UserWalletUpstart::where('user_id', $userId) -> setDec('price', $post['price'])) {
				throw new \Exception('富豪钱包扣款失败');
			}
			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '!");window.history.go(-1);</script>';exit;
		}
		echo '<meta charset="utf-8"><script>alert("' . $resultHelpGetUpstart['message'] . '!");window.location.href="/withdraw/upstart";</script>';exit;
	}

	public function freeze() {
		$userId = \think\Session::get('user_id');
		$this -> view -> assign('userWalletUpstartFreezes', UserWalletUpstartFreeze::where('user_id', $userId) -> order('is_freeze ASC, user_wallet_upstart_freeze_id DESC') -> paginate(10, true));

		$this -> view -> assign('sumUserWalletUpstartFreeze', UserWalletUpstartFreeze::where('user_id', $userId) -> where('is_freeze', 0) -> sum('remain_price'));

		return $this -> view -> fetch();
	}

	public function draw() {
		$get = $this -> request -> get();

		$drawUserWalletUpstartFreezeService = (new UserWalletUpstartFreezeService) -> draw($get['user_wallet_upstart_freeze_id'], $get['key']);
		echo '<meta charset="utf-8"><script>alert("' . $drawUserWalletUpstartFreezeService['message'] . '!");window.location.href="/withdraw/upstart/freeze";</script>';exit;
	}
}
?>